/**
 * @params k - layerId 图层id
 * @params v - visible 控制显示隐藏
 * @params c - color  css的color值string
 * @example { '6fa8d4b9-19c9-46f8-9f1f-dc6d8b6f1044': { visible: true, color: '#00f701' } }
 */
export interface LayerType {
    dataset: number,
    layer: number,
    value: { visible: boolean, color: string | null },
}


/**
 * 默认 替换 混合
 */
export type ColorModeType = "HIGHLIGHT" | "REPLACE" | "MIX"


export const apis = {
    layerconfigfind: `/ts/layer/config/get`,// 获取图层配置
    layerconfigsave: `/ts/layer/config/put`,// 保存图层配置
    featureproperty: `/ts/editor/property`,// 获取属性
    featureeditormuti: `/ts/editor/update`,// 批量编辑要素的空间状态
    getts: "/ts/info",// 获取切片服务的端口
}


/**
 * 获取图层配置
 * @param tileServiceName - 服务名称
 * @returns LayerType - 图层配置
 */
export const _getLayerConfig = async (baseUrl: string, tileServiceName: string) => {
    try {
        const authToken = window.localStorage.getItem('Authorization');
        const requestOptions = {
            headers: {
                ...(authToken ? { 'Authorization': authToken } : {})
            },
            mode: "cors", // 用来决定是否允许跨域请求  

        };
        // @ts-ignore
        const res = await fetch(baseUrl + `${apis.layerconfigfind}?tileServiceName=${tileServiceName}`, requestOptions);
        const jsonStr = await res.text();
        const json = JSON.parse(jsonStr) as LayerType[];
        return json;
    } catch (error) {
        console.error(error);
    }

}

/**
 * 保存图层配置
 * @param tileServiceName - 服务名称
 * @returns LayerType - 图层配置
 */
export const _saveLayerConfig = async (baseUrl: string, tileServiceName: string, layerConfig?: LayerType[]) => {
    try {
        const authToken = window.localStorage.getItem('Authorization');
        const _layerConfig = layerConfig ?? {};
        const requestOptions = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                ...(authToken ? { 'Authorization': authToken } : {})
            },
            mode: "cors", // 用来决定是否允许跨域请求  
            body: JSON.stringify({
                tileServiceName: tileServiceName,
                layerConfig: JSON.stringify(_layerConfig)
            })
        };
        // @ts-ignore

        const res = await fetch(baseUrl + apis.layerconfigsave, requestOptions);
        const jsonStr = await res.text();
        const json = JSON.parse(jsonStr) as any
        return json;
    } catch (error) {
        console.error(error);
    }
}

/**
 * 获取切片服务的端口
 * @param {*} tileServiceName
 * @returns 
 */
export const _getport = async (url: string) => {
    try {
        const authToken = window.localStorage.getItem('Authorization');
        const requestOptions = {
            headers: {
                ...(authToken ? { 'Authorization': authToken } : {}),

            },
            mode: "cors", // 用来决定是否允许跨域请求  

        };
        // @ts-ignore
        const res = await fetch(url, requestOptions);
        const jsonStr = await res.text();
        const json = JSON.parse(jsonStr);
        if (json.status == "ok") {
            return json.data.port as string;
        } else {
            console.error(json.status);
            return undefined
        }
    } catch (error) {
        console.error(error);
    }
}
/**
 * 获取要素属性
 * @param {*} tileServiceName
 * @returns 
 */
export const _getFeatureProperty = async (baseUrl: string, tileServiceName: string, featureId: string) => {
    try {
        const data = { tileServiceName, featureId };
        const token = window.localStorage.getItem("Authorization")
        const req = await fetch(baseUrl + apis.featureproperty, {
            method: "post",
            headers: {
                'Content-Type': 'application/json',
                ...(token ? { 'Authorization': token } : {})
            },
            body: JSON.stringify(data),

            mode: "cors", // 用来决定是否允许跨域请求  

        });
        const jsonStr = await req.text();
        const res = JSON.parse(jsonStr);
        if (res.status == "ok") {
            return res.data as { [k: string]: any };
        } else {
            console.error(res.data.status);
            return undefined
        }
    } catch (error) {
        console.error(error)
    }
}



